﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Red.Launcher.ViewModels.Workspaces.Database
{
    public enum Operand
    {
        [Description( "<" )]
        LessThan,

        [Description( "<=" )]
        LessThanOrEqual,

        [Description( ">" )]
        GreaterThan,

        [Description( ">=" )]
        GreaterThanOrEqual,

        [Description( "==" )]
        EqualTo,
    }

    public class RatingConfigurationGroup : Record
    {
        #region Inherited Properties

        public override Metadata Metadata { get { return METADATA; } }
        public override string Icon { get { return "appbar_graph_line"; } }
        public override IEnumerable<object> Values { get { return null; } }

        #endregion Inherited Properties

        #region Public Properties

        public static Metadata METADATA { get; private set; }

        #endregion Public Properties

        #region C-Tor

        static RatingConfigurationGroup()
        {
            METADATA = new Metadata();
            METADATA.Children.Add( typeof( RatingConfiguration ) );
        }

        public RatingConfigurationGroup()
        {
            Records = new Dictionary<System.Type, Dictionary<int, Record>>();

            IsVirtual = true;
            Name = "Rating Configurations";

            IsWatchingForChanges = true;
        }

        #endregion C-Tor
    }

    public class RatingConfiguration : Record
    {
        #region Inherited Properties

        public override Metadata Metadata { get { return METADATA; } }
        public override string Icon { get { return "appbar_graph_line"; } }
        public override IEnumerable<object> Values { get { return new List<object>() { Name, Threshold, Operand }; } }

        #endregion Inherited Properties

        #region Private Data

        private float _threshold;
        private Operand _operand;

        #endregion Private Data

        #region Public Properties

        public static Metadata METADATA { get; private set; }

        public float Threshold
        {
            get { return _threshold; }
            set
            {
                if ( _threshold != value )
                {
                    _threshold = value;
                    OnPropertyChanged();
                }
            }
        }

        public Operand Operand
        {
            get { return _operand; }
            set
            {
                if ( _operand != value )
                {
                    _operand = value;
                    OnPropertyChanged();
                }
            }
        }

        #endregion Public Properties

        #region C-Tor

        static RatingConfiguration()
        {
            METADATA = new Metadata()
            {
                Table = "StabilityRatings",
                Keys = new List<string>() { "name", "threshold", "operand" },
                CreateDefault = () => { return new RatingConfiguration(); },
                CreateFromDatabaseRow = ( Dictionary<string, object> data ) =>
                {
                    return new RatingConfiguration()
                    {
                        Id = (int)data[ "id" ],
                        Name = (string)data[ "name" ],
                        Threshold = (float)( (double)data[ "threshold" ] ),
                        Operand = (Operand)Enum.Parse( typeof( Operand ), (string)data[ "operand" ] )
                    };
                }
            };
        }

        #endregion C-Tor
    }
}
